package com.ellwood.common.core.exception;

import com.ellwood.common.core.enums.HttpCodeEnum;
import com.ellwood.common.core.result.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @ProjectName: Ellwood-framework-boot
 * @PackageName: com.ellwood.common.core.exception
 * @ClassName: GlobalExceptionHandler
 * @Description: // 自定义全局异常处理
 * @Author: Ellwood
 * @CreateDate: 2024/7/15 - 上午11:50
 * @Version: V1.0.0
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(SystemException.class)
    public ApiResult<?> systemExceptionHandler(SystemException se) {
        log.error("出现了异常: {}", se.getMessage(), se);
        return ApiResult.errorResult(se.getCode(), se.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ApiResult<String> exceptionHandler(Exception ex) {
        log.error("出现了异常: {}", ex.getMessage(), ex);
        return ApiResult.errorResult(HttpCodeEnum.SYSTEM_ERROR.getCode(), ex.getMessage());
    }

}
